package com.ryskoo.permission.menu.repository;

import com.ryskoo.framework.core.repository.BasicJPAImpl;
import com.ryskoo.permission.menu.domain.MenuPO;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class MenuJPAImpl extends BasicJPAImpl<MenuPO> implements IMenuRepository {

	/**
	 * 根据菜单类型查询一级菜单
	 * 
	 * @param menuType
	 * @return
	 */
	@Override
	public List<MenuPO> getMenus(String menuType, String parentId) {
		StringBuffer hql = new StringBuffer("select o from MenuPO o where 1=1");

		// 如果parentId为空，则只查询一级菜单
		if (StringUtils.isEmpty(parentId)) {
			hql.append(" and (o.parentId = '' or o.parentId is null) ");
		} else {
			hql.append(" and o.parentId =:parentId");
		}

		if (!StringUtils.isEmpty(menuType)) {
			hql.append(" and o.type =:type ");
		}

		hql.append(" order by o.position asc");

		Query q = this.getH4Session().createQuery(hql.toString());
		if (!StringUtils.isEmpty(menuType)) {
			q.setInteger("type", Integer.parseInt(menuType));
		}

		if (!StringUtils.isEmpty(parentId)) {
			q.setString("parentId", parentId);
		}

		return q.list();
	}

	/**
	 * 根据菜单类型和父菜单查询菜单的uuid
	 * 
	 * @param menuType
	 * @return
	 */
	@Override
	public List<String> getMenuUuids(String menuType, String parentId) {
		StringBuffer hql = new StringBuffer(
				"select o.uuid from MenuPO o where 1=1");

		// 如果parentId为空，则只查询一级菜单
		if (StringUtils.isEmpty(parentId)) {
			hql.append(" and (o.parentId = '' or o.parentId is null) ");
		} else {
			hql.append(" and o.parentId =:parentId");
		}

		// 根据菜单类型查询
		if (!StringUtils.isEmpty(menuType)) {
			hql.append(" and o.type =:type ");
		}

		hql.append(" order by o.position asc");

		Query q = this.getH4Session().createQuery(hql.toString());
		if (!StringUtils.isEmpty(menuType)) {
			q.setInteger("type", Integer.parseInt(menuType));
		}

		if (!StringUtils.isEmpty(parentId)) {
			q.setString("parentId", parentId);
		}
		return q.list();
	}

	/**
	 * 根据管理员id获取此管理员可见的菜单列表
	 *
	 * @param managerId 管理员id
	 * @param menuType 菜单类型  0：平台菜单  1：商户菜单
     * @return 菜单列表 {@link List}&lt;{@link MenuPO}&gt;
	 */
	@Override
	public List<MenuPO> getMenuByManagerId(String managerId, int menuType) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT menu.* FROM pms_menu menu");
        sql.append(" LEFT JOIN pms_role_menu_rel role_menu ON menu.uuid = role_menu.menu_id AND menu.state = '1'");
        sql.append(" AND menu.type = :menuType");
        sql.append(" LEFT JOIN pms_role role ON role_menu.role_id = role.uuid");
        sql.append(" LEFT JOIN pms_manager_role_rel manager_role ON role.uuid = manager_role.role_id");
        sql.append(" LEFT JOIN pms_manager manager ON manager_role.manager_id = manager.uuid");
        if (StringUtils.isNotBlank(managerId)) {
            sql.append(" WHERE manager.uuid = :managerId");
        }
        sql.append(" order by menu.position");
        Query query = this.getH4Session().createSQLQuery(sql.toString()).addEntity(MenuPO.class);
        query.setInteger("menuType", menuType);
        if (StringUtils.isNotBlank(managerId)) {
            query.setString("managerId", managerId);
        }
		return query.list();
	}

    /**
     * 根据角色id获取此管理员可见的菜单列表
     *
     * @param roleId   角色id
     * @param menuType 菜单类型  0：平台菜单  1：商户菜单
     * @return 菜单列表 {@link List}&lt;{@link MenuPO}&gt;
     */
    @Override
    public List<MenuPO> getMenuByRoleId(String roleId, int menuType) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT menu.* FROM pms_menu menu");
        sql.append(" LEFT JOIN pms_role_menu_rel role_menu ON menu.uuid = role_menu.menu_id AND menu.state = '1'");
        sql.append(" AND menu.type = :menuType");
        sql.append(" LEFT JOIN pms_role role ON role_menu.role_id = role.uuid");
        if (StringUtils.isNotBlank(roleId)) {
            sql.append(" WHERE role.uuid = :roleId");
        }
        sql.append(" order by menu.position");
        Query query = this.getH4Session().createSQLQuery(sql.toString()).addEntity(MenuPO.class);
        query.setInteger("menuType", menuType);
        if (StringUtils.isNotBlank(roleId)) {
            query.setString("roleId", roleId);
        }
        return query.list();
    }
}
